<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!--<el-form-item label="打牌时间" prop="playingTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.playingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择打牌时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="自定义积分模式" prop="customMahjong">
        <el-input
          v-model="queryParams.customMahjong"
          placeholder="请输入自定义积分模式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="比赛麻将积分模式" prop="matchMahjong">
        <el-input
          v-model="queryParams.matchMahjong"
          placeholder="请输入比赛麻将积分模式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="普通麻将积分模式" prop="ordinaryMahjong">
        <el-input
          v-model="queryParams.ordinaryMahjong"
          placeholder="请输入普通麻将积分模式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="美国麻将积分模式" prop="americanMahjong">
        <el-input
          v-model="queryParams.americanMahjong"
          placeholder="请输入美国麻将积分模式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!--  <el-form-item label="会员状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="0正常用户，1非正常用户" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>-->
      <!-- <el-form-item label="车程" prop="drivingDistance">
         <el-input
           v-model="queryParams.drivingDistance"
           placeholder="请输入车程"
           clearable
           size="small"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>-->
      <!--<el-form-item label="生日" prop="birthday">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择生日">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="昵称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="手机号" prop="phone">
         <el-input
           v-model="queryParams.phone"
           placeholder="请输入手机号"
           clearable
           size="small"
           @keyup.enter.native="handleQuery"
         />
       </el-form-item>-->
<!--      <el-form-item label="会员id" prop="memberId">-->
<!--        <el-input-->
<!--          v-model="queryParams.memberId"-->
<!--          placeholder="请输入会员id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mahjong:member:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mahjong:member:edit']"
        >修改
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mahjong:member:edit']"
        >恢复
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
         <el-button
           type="warning"
           icon="el-icon-download"
           size="mini"
           @click="handleExport"
           v-hasPermi="['mahjong:member:export']"
         >导出</el-button>
       </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="卡号" align="center" prop="cardNumber" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="电话" align="center" prop="phone" />

      <el-table-column label="打牌大小" align="center" prop="ordinaryMahjong" :formatter="majiangCoustromFormat" />
      <el-table-column label="储值余额" align="center" prop="storedMoney" />
      <el-table-column label="赠送余额" align="center" prop="sendMoney" />
      <el-table-column label="积分" align="center" prop="integral" />
      <el-table-column label="喜欢" align="center" prop="likes" :formatter="likesFormat"/>
      <el-table-column label="忌讳" align="center" prop="taboo":formatter="tabooFormat" />
      <el-table-column label="自定义标签" align="center" prop="custom":formatter="customFormat" />
      <el-table-column label="不同桌" align="center" prop="different" />
      <el-table-column label="打牌时间" align="center" prop="playingTime" :formatter="playingTimeFormat"/>
      <el-table-column label="车程" align="center" prop="drivingDistance" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="生日" align="center" prop="birthday" width="180"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员"/>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="昵称" prop="name">
          <el-input v-model="form.name" placeholder="请输入昵称"/>
        </el-form-item>
        <el-form-item label="图片" prop="img2">
          <el-upload
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.img2" :src="imgpre +form.img2" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="其他图片" prop="img1">
          <el-upload
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess2"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.img1" :src="imgpre +form.img1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

     <!--   <el-form-item label="会员卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入会员卡号"/>
        </el-form-item>-->
        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车程" prop="drivingDistance">
          <el-input v-model="form.drivingDistance" placeholder="请输入车程"/>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOnlyForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="喜欢标签">
          <el-select v-model="form.likes" multiple placeholder="请选择喜欢标签">
            <el-option
              v-for="dict in likesOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="忌讳标签">
          <el-select v-model="form.taboo" multiple placeholder="请选择忌讳标签">
            <el-option
              v-for="dict in tabooOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员描述">
          <el-select v-model="form.custom"  multiple placeholder="请选择会员描述">
            <el-option
              v-for="dict in customOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="不同桌">
          <el-select v-model="form.different"  multiple placeholder="请选择不同桌">
            <el-option
              v-for="dict in zlList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="美国麻将积分">
          <el-select v-model="form.americanMahjong"  multiple placeholder="请选择美国麻将积分">
            <el-option
              v-for="dict in americanMahjongOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="普通麻将积分">
          <el-select v-model="form.ordinaryMahjong" multiple  placeholder="请选择普通麻将积分">
            <el-option
              v-for="dict in ordinaryMahjongOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="比赛麻将积分">
          <el-select v-model="form.matchMahjong" multiple placeholder="请选择比赛麻将积分">
            <el-option
              v-for="dict in matchMahjongOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="自定义积分">
          <el-select v-model="form.customMahjong" multiple placeholder="请选择自定义积分">
            <el-option
              v-for="dict in customMahjongOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打牌时间">
          <el-select v-model="form.playingTime" multiple placeholder="请选择打牌时间">
            <el-option
              v-for="dict in playingTimeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMember,
  getMember,
  getzi,
  delMember,
  addMember,
  updateMember,
  exportMember,
  rollbackMember,
  getzi2
} from "@/api/mahjong/member";
  import defaultSettings from '@/settings'
  import {handerImg,handlerdata} from "@/api/mahjong/utils";

  export default {
    name: "Member",
    data() {
      return {
        uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
        imgpre: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 会员管理表格数据
        memberList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open2: false,
        // 状态 0正常用户，1非正常用户字典
        statusOptions: [],
        // 喜欢标签字典
        likesOptions: [],
        // 忌讳标签字典
        tabooOptions: [],
        // 自定义标签字典
        customOptions: [],
        // 美国麻将积分模式字典
        americanMahjongOptions: [],
        // 普通麻将积分模式字典
        ordinaryMahjongOptions: [],
        // 比赛麻将积分模式字典
        matchMahjongOptions: [],
        // 自定义积分模式字典
        customMahjongOptions: [],
        // 打牌时间字典
        playingTimeOptions: [],
        zlList:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          playingTime: undefined,
          customMahjong: undefined,
          matchMahjong: undefined,
          ordinaryMahjong: undefined,
          americanMahjong: undefined,
          status: undefined,
          drivingDistance: undefined,
          birthday: undefined,
          cardNumber: undefined,
          phone: undefined,
          memberId: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("member_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("like").then(response => {
        this.likesOptions = response.data;
      });
      this.getDicts("nolike").then(response => {
        this.tabooOptions = response.data;
      });
      this.getDicts("custom").then(response => {
        this.customOptions = response.data;
      });
      this.getDicts("america").then(response => {
        this.americanMahjongOptions = response.data;
      });
      this.getDicts("common").then(response => {
        this.ordinaryMahjongOptions = response.data;
      });
      this.getDicts("competition").then(response => {
        this.matchMahjongOptions = response.data;
      });
      this.getDicts("custom_integral").then(response => {
        this.customMahjongOptions = response.data;
      });
      this.getDicts("playing_time").then(response => {
        this.playingTimeOptions = response.data;
      });
      this.getzi();
    },
    methods: {
      getzi(){
        getzi2().then(response => {
          this.zlList = response.data;

        });
      },
      majiangCoustromFormat(row, column) {
        return handlerdata(row.ordinaryMahjong, this.ordinaryMahjongOptions);
      },

      tabooFormat(row, column) {
        return handlerdata(row.taboo,this.tabooOptions);
      },
      customFormat(row, column) {
        return handlerdata(row.custom,this.customOptions);
      },
      likesFormat(row, column) {
        return handlerdata(row.likes,this.likesOptions);
      },
      playingTimeFormat(row, column) {
        return handlerdata(row.playingTime, this.playingTimeOptions);
      },
      showImg(e) {
        const array = [];
        array.push(this.imgpre + e.img2)
        array.push(this.imgpre + e.img1)
        return array;
      },
      handleAvatarSuccess(res) {
        this.form.img2 = res.fileName
      },
      handleAvatarSuccess2(res) {
        this.form.img1 = res.fileName
      },

      beforeAvatarUpload(file) {


        const isJPG = handerImg(file.type);

        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      /** 查询会员管理列表 */
      getList() {
        this.loading = true;
        this.queryParams.isDel='1'
        listMember(this.queryParams).then(response => {
          this.memberList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态 0正常用户，1非正常用户字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.open2 = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          updateTime: undefined,
          updateBy: undefined,
          createTime: undefined,
          createBy: undefined,
          playingTime: [],
          customMahjong: [],
          matchMahjong: [],
          ordinaryMahjong: [],
          americanMahjong: [],
          different: [],
          custom: [],
          taboo: [],
          likes: [],
          status: "0",
          remark: undefined,
          drivingDistance: undefined,
          birthday: undefined,
          cardNumber: undefined,
          img2: undefined,
          img1: undefined,
          name: undefined,
          phone: undefined,
          memberId: undefined,
          id: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加会员管理";
      },
      handleUpdateOther(row) {
        this.reset();
        const id = row.id || this.ids
        getMember(id).then(response => {
          this.form = response.data;
          this.form.likes=this.coverList(this.form.likes);
          this.form.americanMahjong=this.coverList(this.form.americanMahjong);
          this.form.custom=this.coverList(this.form.custom);
          this.form.customMahjong=this.coverList(this.form.customMahjong);
          this.form.different=this.coverList(this.form.different);
          this.form.matchMahjong=this.coverList(this.form.matchMahjong);
          this.form.ordinaryMahjong=this.coverList(this.form.ordinaryMahjong);
          this.form.playingTime=this.coverList(this.form.playingTime);
          this.form.taboo=this.coverList(this.form.taboo);
          this.open2 = true;
          this.title = "会员特点维护";
        });
      },
      coverList(e){
        if(e){
          return e.split(",");
        }
        return e
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getMember(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改会员管理";
        });
      },
      submitOnlyForm: function () {
        if(this.form.likes){
          this.form.likes=this.form.likes.toString()
        }
        if(this.form.americanMahjong){
          this.form.americanMahjong=this.form.americanMahjong.toString()
        }
        if(this.form.custom){
          this.form.custom=this.form.custom.toString()
        }
        if(this.form.customMahjong){
          this.form.customMahjong=this.form.customMahjong.toString()

        }
        if(this.form.different){
          this.form.different=this.form.different.toString()
        }
        if(this.form.matchMahjong){
          this.form.matchMahjong=this.form.matchMahjong.toString()
        }
        if(this.form.ordinaryMahjong){
          this.form.ordinaryMahjong=this.form.ordinaryMahjong.toString()
        }
        if(this.form.playingTime){
          this.form.playingTime=this.form.playingTime.toString()
        }

        if(this.form.taboo){
          this.form.taboo=this.form.taboo.toString()
        }
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.open2 = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.open2 = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        if(this.form.likes){
          this.form.likes=this.form.likes.join(",");
        }
        if(this.form.americanMahjong){
          this.form.americanMahjong=this.form.americanMahjong.join(",");
        }
        if(this.form.custom){
          this.form.custom=this.form.custom.join(",");
        }
        if(this.form.customMahjong){
          this.form.customMahjong=this.form.customMahjong.join(",");

        }
        if(this.form.different){
          this.form.different=this.form.different.join(",");
        }
        if(this.form.matchMahjong){
          this.form.matchMahjong=this.form.matchMahjong.join(",");
        }
        if(this.form.ordinaryMahjong){
          this.form.ordinaryMahjong=this.form.ordinaryMahjong.join(",");
        }
        if(this.form.playingTime){
          this.form.playingTime=this.form.playingTime.join(",");
        }

        if(this.form.taboo){
          this.form.taboo=this.form.taboo.join(",");
        }

        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.open2 = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addMember(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.open2 = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认恢复的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return rollbackMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有会员管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
